<template>
	<div class="row"  v-loading="loading2" element-loading-text="拼命加载中" style="width: 100%;">
		<div class="col-xs-12">
		<div id="role-scheduling-tab">
			<el-button :plain="true" type="danger"  class="againBut" @click="againBut">重新排课</el-button>
			<span class="lookError" @click="lookError">点击查看规则冲突列表</span>
			<div class="role-tab noBorderBottom">
				<ul>
					<li class="no-before room-tab active">
						<i class="icon-step3-std-yes dh"></i><i class="icon-step3-std-no"></i><router-link  :to="{ path: '/automatic/Student' }" >学生课表</router-link>
					</li>
					<li class="">
						<i class="icon-step3-teacher-yes"></i><i class="icon-step3-teacher-no"></i><router-link :to="{ path: '/automatic/Teacher' }" >教师课表</router-link>
					</li>
					<li  class="student-tab" v-if="showClassroom">
						<i class="icon-step3-class-yes"></i><i class="icon-step3-class-no"></i><router-link :to="{ path: '/automatic/Classroom' }">教室课表</router-link>
					</li>
					<li class="">
						<i class="icon-step3-all-yes"></i><i class="icon-step3-all-no"></i><router-link   :to="{ path: '/automatic/MasterTimetable' }" >总课表</router-link>
					</li>
				</ul>
			</div>
			</div>
			<el-select v-model="ClassValue" placeholder="请选择班级" class="selectData" @change="changeClass">
				<el-option v-for="item in getClassList" :key="item.id" :label="item.name" :value="item.name" >
				</el-option>
			</el-select>
			<el-select v-model="StudentValue" placeholder="请选择学生" class="selectData" @change="changeStudent">
				<el-option v-for="item in StudentList" :key="item.studentNo" :label="item.studentName" :value="item.studentNo" >
				</el-option>
			</el-select>
			<div class="block textAndBtn">
				<span class="demonstration">{{StudentName}}课程表</span>
				<span class="wrapper export" @click="exportTable">
				    <el-button type="primary" icon="upload2">导出所有学生课程表</el-button>
				  </span>
				<div class="clear"></div>
			</div>
			<el-table :data="getCourseList" border style="width: 100%">
				<el-table-column prop="num" label="节次" align="center" >
				</el-table-column>
				<el-table-column prop="Monday" v-if="showDay1" label="星期一" align="center">
				</el-table-column>
				<el-table-column prop="Tuesday" v-if="showDay2" label="星期二" align="center">
				</el-table-column>
				<el-table-column prop="Wednesday" v-if="showDay3" label="星期三" align="center">
				</el-table-column>
				<el-table-column prop="Thursday" v-if="showDay4" label="星期四" align="center">
				</el-table-column>
				<el-table-column prop="Friday" v-if="showDay5" label="星期五" align="center">
				</el-table-column>
				<el-table-column prop="Saturday" v-if="showDay6" label="星期六" align="center">
				</el-table-column>
				<el-table-column prop="Sunday" v-if="showDay7" label="星期天" align="center">
				</el-table-column>
			</el-table>
			<el-dialog title="规则冲突列表" :visible.sync="dialogTableVisible" class="dialogError" v-loading="loadingError" element-loading-text="拼命加载中" style="width: 100%;">
				<el-col :span="24">
						<div class="grid-content bg-purple">
							<div v-for="o in errorList" :key="o" class="text item">{{o}}</div>
						</div>
				</el-col>
			</el-dialog>
		</div>
	</div>
</template>

<script>
	import utils from '@/assets/js/urlConfig.js'
	export default {
		data() {
			return {
				getClassList: [],
				ClassValue: "",
				ClassId:"",
				CsType:"",
				classType:"",
				StudentValue:"",
				StudentNo:"",
				StudentName:"……",
				getCourseList: [],
				StudentList: [],
				taskId:"",
				tnId:"",
				type:"student",
				loading2:true,
				showDay1:false,
				showDay2:false,
				showDay3:false,
				showDay4:false,
				showDay5:false,
				showDay6:false,
				showDay7:false,
				showClassroom:false,
				dialogTableVisible:false,
				errorList:[],
				loadingError:false,
			}
		},
		mounted() {
			this.getSessionData();
			this.getClassListData();
		},
		methods: {
			// 获取班级
			getClassListData() {
				 this.$http.get(utils.UrlConfig.RuleGetClassList+"&taskId="+this.taskId)
				   .then(res => {
				     res = res.data
				     if (res.rtnCode === "0000000") {
				         this.getClassList = res.bizData;
								 this.ClassValue = this.getClassList[0].name;
				     } else {
				         this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
			},
			//改变班级
			changeClass(value){
//				console.log(this.getClassList);
				this.getClassList.forEach(function(element) {
            if (element.name == value) {
                 this.CsType = element.classType;
                 this.ClassId = element.id;
            }
            return;
        }, this);
				if(this.CsType == "行政班"){
					this.classType = 0;
				}else{
					this.classType = 1;
				}
				this.getStudent(this.taskId,this.tnId,this.ClassId,this.classType);
			},
			//查询学生
			getStudent(taskId,tnId,classId,classType){
				this.loading2 = true;
				this.$http.get(utils.UrlConfig.queryStudent+"&taskId="+this.taskId+"&tnId="+this.tnId+"&classId="+classId+"&classType="+classType)
				   .then(res => {
				     res = res.data
				     if (res.rtnCode === "0000000") {
				     		if(res.bizData.length == 0){
				     			this.StudentValue = "";
				     			this.StudentList = [];
				     			this.getCourseList = [];
				     			this.loading2 = false;
				     			return false;
				     		}
				         this.StudentList = res.bizData;
				         this.StudentValue = res.bizData[0].studentNo;
				     } else {
				         this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
			},
			changeStudent(value){
				this.studentNo = value;
				this.StudentList.forEach(function(element) {
            if (element.studentNo == value) {
                 this.StudentName = element.studentName;
            }
            return;
        }, this);
				this.getResult(this.type,this.taskId,this.tnId,this.studentNo);
			},
			//获取课表
			getResult(type,taskId,tnId,studentNo){
				if(studentNo != ""){
					this.loading2 = true;
					this.$http.get(utils.UrlConfig.getStudentCourse+"&taskId="+taskId+"&tnId="+tnId+"&param={studentNo:"+studentNo+"}")
				   .then(res => {
				     res = res.data
				     if (res.rtnCode === "0000000") {
				     		var curriculumList = [];
				     		for(var i = 0 ; i < res.bizData.result.week.length ; i++){
									var str = "showDay"+(i+1);
									this[str] = true;
				     		}
				        for(var i = 0 ; i < res.bizData.result.week[0].length; i++){
									var json = {};
									json.num = "第"+parseInt(i+1)+"节课";
									for(var k = 0 ; k < res.bizData.result.week.length ; k++){
										if(k == 0){
											json.Monday = res.bizData.result.week[k][i];
										}else if(k == 1){
											json.Tuesday = res.bizData.result.week[k][i];
										}else if(k == 2){
											json.Wednesday =  res.bizData.result.week[k][i];
										}else if(k == 3){
											json.Thursday = res.bizData.result.week[k][i];
										}else if(k == 4){
											json.Friday = res.bizData.result.week[k][i];
										}else if(k == 5){
											json.Saturday = res.bizData.result.week[k][i];
										}else if(k == 6){
											json.Sunday = res.bizData.result.week[k][i];
										}
									}
									curriculumList.push(json);
								}
								this.getCourseList = curriculumList;
								this.loading2 = false;
				     } else {
				         this.$message.error(res.msg);
				     }
				 }).catch(error => {
				     console.log(error)
				 })
				}
			},
			exportTable(){
				window.location.href = utils.UrlConfig.exportStudent+"&taskId="+this.taskId+"&tnId="+this.tnId;
			},
			getSessionData(){
				this.taskId = sessionStorage.getItem('taskId');
				this.tnId = sessionStorage.getItem('tnId');
				if(sessionStorage.getItem('classType')!= 1 || sessionStorage.getItem('gradeName')!="高一年级"){
						this.showClassroom = true
				}
			},
			againBut(){
				sessionStorage.setItem('canAgain', "1");
				this.$router.push({
					path: '../automatic/'
				});
			},
			lookError(){
				this.errorList =[];
				this.loadingError = true;
				this.dialogTableVisible = true;
				this.$http.post(utils.UrlConfig.findRule+"&taskId="+this.taskId+"&tnId="+this.tnId)
					.then(res => {
						res = res.data
						if (res.rtnCode === "0000000") {
							this.loadingError = false;
							this.errorList = res.bizData;
						} else {
								this.$message.error(res.msg);
						}
				}).catch(error => {
						console.log(error)
				})
			}
		}
	}
</script>

<style>
	.textAndBtn {
		margin: 20px 0;
	}

	.selectData{
		margin-top: 25px;
	}

	.demonstration {
		font-weight: bold;
		font-size: 14px;
		line-height: 36px;
	}

	.export {
		float: right;
		width: 15%;
	}

	.clear {
		clear: both;
	}

	.againBut{
		width: 122px;
		padding: 8px 0;
		margin-bottom: 35px;
	}

	#role-scheduling-tab .role-tab ul .active a{
		color: #36c6d3;
	}

	#role-scheduling-tab .role-tab ul li a{
		color: #9B9B9B;
	}

	#role-scheduling-tab .lookError{
		cursor: pointer;
	}

		#role-scheduling-tab .lookError:hover{
					color: #36c6d3;
					text-decoration:underline;
		}

		#role-scheduling-tab .dialogError  .text {
	    font-size: 14px;
	  }

		#role-scheduling-tab .dialogError .item {
	    padding: 14px;
	    text-align: left;
	  }
</style>
